If a mutable collection type is used but no mutating functions such as add
or remove
are ever called, and the collection
instance does not leave the scope of the function, it can be replaced with the corresponding immutable collection type.
This is similar to why val
should be used instead of var
for local variables that are never re-assigned.
What is the potential impact?
Readability and Understanding
If an immutable collection type is used, it is evident to the readers that its content is never changed. This makes it easier to understand the
code because readers do not need to keep track of possible state changes of the collection.
Performance
In some cases, optimized implementation variants of collection classes can be used when the collection is immutable.
Wrong code
Developers might intend for a collection to remain unchanged and have their code relying on that constraint. For example, a map could be expected
to contain specific elements. Changing the contents of a collection breaks that constraint. Also, users of an API might otherwise downcast an
immutable collection they got from a library into a mutable collection, and so cause unforeseen side effects.
Declare collections that remain unchanged as immutable to avoid these mistakes.